package com.jneagle.xlstool.bzwgjmx.impl.handler;

import com.dwarfeng.subgrade.stack.exception.HandlerException;
import com.jneagle.xlstool.bzwgjmx.stack.bean.entity.ExportData;
import com.jneagle.xlstool.bzwgjmx.stack.bean.entity.RawPart;
import com.jneagle.xlstool.bzwgjmx.stack.bean.entity.RawPartData;
import com.jneagle.xlstool.bzwgjmx.stack.bean.entity.StructuredData;
import com.jneagle.xlstool.bzwgjmx.stack.handler.DataConfirmHandler;
import com.jneagle.xlstool.bzwgjmx.stack.service.*;
import com.jneagle.xlstool.bzwgjmx.stack.structure.ProgressStatus;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Component
public class DataConfirmHandlerImpl extends AbstractProgressHandler implements DataConfirmHandler {

    private final StructuredDataMaintainService structuredDataMaintainService;
    private final RawPartMaintainService rawPartMaintainService;
    private final RawPartDataMaintainService rawPartDataMaintainService;
    private final ExportDataMaintainService exportDataMaintainService;
    private final ExportErrorInfoMaintainService exportErrorInfoMaintainService;

    public DataConfirmHandlerImpl(
            StructuredDataMaintainService structuredDataMaintainService,
            RawPartMaintainService rawPartMaintainService,
            RawPartDataMaintainService rawPartDataMaintainService,
            ExportDataMaintainService exportDataMaintainService,
            ExportErrorInfoMaintainService exportErrorInfoMaintainService
    ) {
        this.structuredDataMaintainService = structuredDataMaintainService;
        this.rawPartMaintainService = rawPartMaintainService;
        this.rawPartDataMaintainService = rawPartDataMaintainService;
        this.exportDataMaintainService = exportDataMaintainService;
        this.exportErrorInfoMaintainService = exportErrorInfoMaintainService;
    }

    @Override
    public void confirm() throws HandlerException {
        try {
            // 广播进度变更事件。
            fireProgressChanged(ProgressStatus.UNCERTAIN);

            // 清空相关实体的维护服务。
            exportDataMaintainService.clear();
            exportErrorInfoMaintainService.clear();

            List<ExportData> exportDataList;
            AtomicInteger rowIndex = new AtomicInteger();

            // 获取所有的结构化数据，转换为导出数据，添加至导出数据的维护服务。
            List<StructuredData> structuredDataList = structuredDataMaintainService.lookupAsList();
            exportDataList = structuredDataList.stream().map((data) -> new ExportData(
                    null, data.getProjectNo(), data.getPartNo(), data.getClassificationName(),
                    data.getProductionName(), data.getSpecification(), data.getDemand(), data.getRemark(),
                    rowIndex.incrementAndGet(), true
            )).collect(Collectors.toList());
            exportDataMaintainService.batchInsert(exportDataList);

            // 获取所有追加标记为 true 的原始数据。
            List<RawPart> rawPartList = rawPartMaintainService.lookupAsList(
                    RawPartMaintainService.APPEND, new Object[0]
            );
            for (RawPart rawPart : rawPartList) {
                // 获取父项为 rawPart 的所有 RawPartData，转换为导出数据，添加至导出数据的维护服务。
                List<RawPartData> rawPartDataList = rawPartDataMaintainService.lookupAsList(
                        RawPartDataMaintainService.CHILD_FOR_RAW_PART, new Object[]{rawPart.getKey()}
                );
                exportDataList = rawPartDataList.stream().map(data -> new ExportData(
                        null, data.getProjectNo(), data.getPartNo(), data.getClassificationName(),
                        data.getProductionName(), data.getSpecification(), data.getDemand(), data.getRemark(),
                        rowIndex.incrementAndGet(), false
                )).collect(Collectors.toList());
                exportDataMaintainService.batchInsert(exportDataList);
            }
        } catch (Exception e) {
            throw new HandlerException(e);
        } finally {
            // 广播进度变更事件。
            fireProgressChanged(ProgressStatus.IDLE);
        }
    }
}
